package com.yjq.yys.yhCalculate.utils;

import com.yjq.yys.yhCalculate.enums.Symbol;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.text.NumberFormat;

@Slf4j
public class ExpressionUtil {


    public static Double result(String str) {

        str = str.replaceAll(" ","");
        if (str.startsWith(Symbol.Minus.getName())) {
            str = "0" + str;
        }
        return Double.valueOf(replaceParentheses(str));
    }

    private static String replaceParentheses(String str1) {

        int end, begin = str1.lastIndexOf(Symbol.Left.getName());

        if (begin > -1) {
            end = str1.indexOf(Symbol.Right.getName(), begin);
            str1 = str1.substring(0, begin) + calculate(str1.substring(begin + 1, end)) + str1.substring(end + 1);
            return replaceParentheses(str1);
        } else {
            return calculate(str1)+"";
        }
    }

    private static Double calculate(String str2) {
        Double[] plusR, multiplyR, minusR;
        Double temp;
        String[] multiply, divide, minus;

        String[] plus = str2.split(Symbol.Plus2.getName());
        plusR = new Double[plus.length];

        for (int m = 0; m < plus.length; m++) {

            minus = plus[m].split(Symbol.Minus2.getName());
            minusR = new Double[minus.length];

            for (int z = 0; z < minus.length; z++) {

                multiply = minus[z].split(Symbol.Multiply2.getName());
                multiplyR = new Double[multiply.length];

                for (int j = 0; j < multiply.length; j++) {

                    divide = multiply[j].split(Symbol.Divide2.getName());
                    /*
                     * 初始化临时值。
                     */
                    temp = StringUtils.isEmpty(divide[0]) ? 0D : Double.valueOf(divide[0]);
                    /*
                     * 遍历除数组，得出某个乘字符串中的除法结果。
                     */
                    for (int i = 1; i < divide.length; i++) {
                        temp /= Float.valueOf(divide[i]);
                    }
                    /*
                     * 将结果存入乘法数组。
                     */
                    multiplyR[j] = temp;

                }

                /*
                 * 遍历乘数组，得出某个减字符串中的乘法结果。
                 */
                temp = 1D;
                for (Double multis : multiplyR) {
                    temp *= multis;
                }
                minusR[z] = temp;

            }

            /*
             * 遍历减数组，得出某个加字符串中的减法结果。
             */
            temp = minusR[0];
            for (int mins = 1; mins < minusR.length; mins++) {
                temp -= minusR[mins];
            }
            plusR[m] = temp;
        }

        /*
         * 遍历加数组，得出最终结果。
         */
        temp = 0D;
        for (Double pluss : plusR) {
            temp += pluss;
        }
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        return Double.valueOf(nf.format(temp));

    }

    public static void main(String[] args) {
        Double a = result("178.51420000000002 * 6698.506829380017 * ((1 - 1.001115436490116) + 1.001115436490116 * 1.6136724)");
        System.out.println(1 - 0.9994422414901161);
        System.out.println(a);
    }
}
